//
//  FormatChecker.m
//  HuiMaiMall
//  有效性校验
//  Created by 刘 璨 on 12-7-27.
//  Copyright (c) 2012年 app梦工厂. All rights reserved.
//

#import "FormatChecker.h"
#import "RegexKitLite.h"

#define myDotNumbers     @"0123456789.\n"
#define myNumbers     @"0123456789"

//只能输入字母和数字
#define kAlphaNum @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

@implementation FormatChecker

+(BOOL)validateWalletAddr:(NSString *)addr
{
    return [addr isMatchedByRegex:@"^0x[0-9A-Fa-f]{40}"];
}

+ (BOOL)validateMobile:(NSString *)mobile {
    return [mobile isMatchedByRegex:@"^[0-9]*"];
}

+ (BOOL)validateTelephone:(NSString *)telephone {
    NSInteger occurenceCount = 0;
    for (int i=0; i<telephone.length; i++) {
        if ([telephone characterAtIndex:i] == '-') {
            occurenceCount++;
        }
    }
    if (occurenceCount > 1) {
        return FALSE;
    } else if (occurenceCount == 1) {
        if ([telephone rangeOfString:@"-"].location!=3 && [telephone rangeOfString:@"-"].location!=4) {
            return FALSE;
        }
    }
    NSString* numberString = [telephone stringByReplacingOccurrencesOfString:@"-" withString:@""];
    return (numberString.length==11 && [numberString isMatchedByRegex:@"0[1-9]{1}[0-9]{9}"]) || (numberString.length==8 && [numberString isMatchedByRegex:@"[1-9]{1}[0-9]{7}"]);
}

+ (BOOL)validatePostage:(NSString *)postage {
    return postage.length==6 && [postage isMatchedByRegex:@"^[0-9]+${6}"];
}

+ (BOOL)validatePassword:(NSString*)pwd
{
    if ([pwd isMatchedByRegex:@"^[a-zA-Z0-9.*^~!@#$%&()+|_,:;<>=?\"\'\\/{}]{6,20}+$"]) {
        return YES;
    }else {
        return NO;
    }
}

+ (BOOL)validateEmail:(NSString*)email {
    
    if ([email isMatchedByRegex:@"^[0-9a-zA-Z]+@(([0-9a-zA-Z]+)[.])+[a-z]{2,4}"]) 
    {
        return YES;
    }else {
        return NO;
    }
    
    
    
    if( (0 != [email rangeOfString:@"@"].length) &&  (0 != [email rangeOfString:@"."].length) )
    {
        NSMutableCharacterSet *invalidCharSet = [[[NSCharacterSet alphanumericCharacterSet] invertedSet]mutableCopy];
        [invalidCharSet removeCharactersInString:@"_-"];
        
        NSRange range1 = [email rangeOfString:@"@" options:NSCaseInsensitiveSearch];
        
        // If username part contains any character other than "."  "_" "-"
        
        NSString *usernamePart = [email substringToIndex:range1.location];
        NSArray *stringsArray1 = [usernamePart componentsSeparatedByString:@"."];
        for (NSString *string in stringsArray1) {
            NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet: invalidCharSet];
            if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])
                return NO;
        }
        
        NSString *domainPart = [email substringFromIndex:range1.location+1];
        NSArray *stringsArray2 = [domainPart componentsSeparatedByString:@"."];
        
        for (NSString *string in stringsArray2) {
            NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet:invalidCharSet];
            if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])
                return NO;
        }
        NSString* topDomainPart = [domainPart substringFromIndex:
                                   [domainPart rangeOfString:@"." options:NSCaseInsensitiveSearch].location];
        NSSet * TLD= [NSSet setWithObjects:@".com",@".edu",@".gov",@".cn", nil];
        if (topDomainPart !=nil && ([TLD containsObject:topDomainPart])) {
            return YES;
        }
        else {
            return NO;
        }
        
        return YES;
    }
    else // no ''@'' or ''.'' present
        return NO;
}

+ (BOOL)validateTaxerIdentifier:(NSString *)identifier {
    return identifier.length==15 && [identifier isMatchedByRegex:@"[0-9]{15}"];
}

+ (BOOL)validateBankAccount:(NSString *)account {
    return (account.length==16 && [account isMatchedByRegex:@"[0-9]{16}"]) || (account.length==19 && [account isMatchedByRegex:@"[0-9]{19}"]);
}

+ (BOOL)validateReceiver:(NSString *)info
{
    //汉字、字母、中划线、下划线、实点组合，4-50字符组成。
    if([info rangeOfString:@"&"].location != NSNotFound)
    {
        return NO;
    }
    return YES;
}
//纯数字
+(BOOL)validateGoodsId:(NSString *)info
{
    if([info isMatchedByRegex:@"^[0-9]+"])
    {
        return YES;
    }
    return NO;
}
//条形码十三位数字判断
+(BOOL)validateBarCode:(NSString *)info
{
    
    if([info isMatchedByRegex:@"^[0-9]{13}"])
    {
//        if (info.length==13) {
//            NSLog(@"13位");
//            return YES;
//        }
        return YES;
        
    }
    return NO;
}

//沱沱
+(BOOL)validateTooToo:(NSString *)info
{
    NSString *tooStr = [[info substringToIndex:6] lowercaseString];
    
    if ([tooStr isEqualToString:@"tootoo"]) {
        
        return YES;
    }
    
    return NO;
}
//web判断
+(BOOL)validateWeb:(NSString *)info
{
    NSString *tooStr = [[info substringToIndex:4] lowercaseString];
    
    if ([tooStr isEqualToString:@"http"]) {
        
        return YES;
    }

    return NO;
}


//校验图片链接
+(BOOL)validateHttp:(NSString *)http
{
    NSString *url = @"";
    if (http.length >7) {
        url = [[http substringToIndex:7] lowercaseString];
    }

    if ([url rangeOfString:@"http://"].location != NSNotFound) {
        
        return YES;
    }
    
    return NO;
}

+(BOOL)validateDate:(NSString *)info
{
    
    return info.length==10 && [info isMatchedByRegex:@"[0-9]{4}-[0-9]{2}-[0-9]{2}"];
}
+(NSString *)getSecurityPhoneNum:(NSString *)phoneNum
{
    if ([self validateMobile:phoneNum] ) {
        return [NSString stringWithFormat:@"%@****%@",[phoneNum substringToIndex:3],[phoneNum substringFromIndex:7]];
    }
    
    return phoneNum;
    
}

+(BOOL)validateNumber:(NSString *)info
{
    //输入的内容必须为数字和小数点
    NSCharacterSet *cs= [[NSCharacterSet characterSetWithCharactersInString:myNumbers]invertedSet];
    NSString *filtered = [[info componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    
    BOOL basicTest = [info isEqualToString:filtered];
    
    if (!basicTest) {
        
        return NO;
    }
    
    return YES;
}

+(BOOL)validatePrice:(NSString *)info
{
    //输入的内容必须为数字和小数点
    NSCharacterSet *cs= [[NSCharacterSet characterSetWithCharactersInString:myDotNumbers]invertedSet];
    NSString *filtered = [[info componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    
    BOOL basicTest = [info isEqualToString:filtered];
    
    if (!basicTest) {
        
        return NO;
    }
    
    return YES;
}

+(BOOL)validateChargeTextField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //输入的内容必须为数字和小数点
    NSCharacterSet *cs= [[NSCharacterSet characterSetWithCharactersInString:myDotNumbers]invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    
    BOOL basicTest = [string isEqualToString:filtered];
    
    if (!basicTest) {
        
        return NO;
    }
    
    
    //不能输入两个.
    NSUInteger nDotLoc = [textField.text rangeOfString:@"."].location;
    if (NSNotFound != nDotLoc && [string isEqualToString:@"."]) {
        
        return NO;
    }
    
    if (NSNotFound == nDotLoc && 0 != range.location && ![string isEqualToString:@""]&&![string isEqualToString:@"."]) {
        
        if (textField.text.length>=5) {  //小数点前面5位
            
            return NO;
        }
    }
    
    //小数点后只能保留两位
    if (NSNotFound != nDotLoc && range.location > nDotLoc +2) {
        
        return NO;
        
    }
    
    return YES;
}
+(BOOL)validateAlphabetAndNumber:(NSString *)info
{
    //只能输入字母和数字
    NSCharacterSet *cs;
    
    cs = [[NSCharacterSet characterSetWithCharactersInString:kAlphaNum] invertedSet];
    
    //按cs分离出数组,数组按@""分离出字符串
    NSString *filtered = [[info componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    
    return [info isEqualToString:filtered];
    
}
@end
